package com.xdf.xzymanagementsystem.util;

import com.xdf.xzymanagementsystem.entity.SysUser;
import com.xdf.xzymanagementsystem.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Date;

/**
 * @author chanchaw
 * @create 2022-11-19 8:48
 */
@Slf4j
@Service
public class SystemUtils {
    @Value("${globalv.server}")
    private String server;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 检测传入的日期是否可以作为单据日期：
     * 保存单据时的“单据日期”不是当日的日期则返回false
     * 由于前端操作人员决定是否继续保存
     * @param billDate 前端传递来的单据日期
     * @return true表示不需要弹出提示，false要弹提示要求用户选择是否继续保存
     */
    public static Boolean checkBillDate(Date billDate){
        return DatetimeUtils.isSameDate(billDate,new Date());
    }

    public SysUser getLoginUser(){
        Integer userPKInteger = ShiroUtils.getUserPKInteger();
        SysUser sysUser = null;
        try {
            sysUser = sysUserService.selectByPrimaryKey(userPKInteger);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return sysUser;
    }

    public String getLoginUsername(){
        SysUser loginUser = getLoginUser();
        return loginUser.getUserName();
    }

    @Async("ccAlarmThreadPool")  //ccEmailThreadPool即配置线程池的方法名，此处如果不写自定义线程池的方法名，会使用默认的线程池
    public void doTask1(int i) {
        log.info("Task"+i+" started.");
    }

    public String appenderServerAndTime(String content){
        content = content + "\n服务器：" + server + "，创建时间：" + DatetimeUtils.formatNow();
        return content;
    }
}
